package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.*;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DistrictMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 用户详情接口,只返回用户信息
 * 
 * @author 甄玉晴
 *
 */
public class UserInfoQueryByUserNo implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			MessageBean<UserBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功",
					UserBean.class);
			UserBean userBean = JsonUtils.objectToPojo(req.getData(), UserBean.class);
			if(userBean==null||StringUtils.isEmpty(userBean.getUserNo())) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "用户编号不能为空。", Void.class);
			}
			UserInfoMapper mapper = factory.getMapper(UserInfoMapper.class);
			UserBean user = mapper.getUserInfoByUserNo(userBean.getUserNo(),UserBean.USER);// 获取用户信息
			// 获取数据字典信息
			if (StringUtils.isNotBlank(user.getStatus())) {
				user.setStatus(Tools.getDicNameByCodeAndValue(factory, "CUS", user.getStatus()));
			}
			if (StringUtils.isNotBlank(user.getMeterReadingCycle())) {
				user.setMeterReadingCycle(Tools.getDicNameByCodeAndValue(factory, "RTP", user.getMeterReadingCycle()));
			}
			if (StringUtils.isNotBlank(user.getUseWaterObject())) {
				user.setUseWaterObject(Tools.getDicNameByCodeAndValue(factory, "UWS", user.getUseWaterObject()));
			}
			if (StringUtils.isNotBlank(user.getMeterUse())) {
				user.setMeterUse(Tools.getDicNameByCodeAndValue(factory, "CMC", user.getMeterUse()));
			}
			if (StringUtils.isNotBlank(user.getSplitMeterWay())) {
				user.setSplitMeterWay(Tools.getDicNameByCodeAndValue(factory, "MRM", user.getSplitMeterWay()));
			}
			if (StringUtils.isNotBlank(user.getSplitMeterWay())) {
				user.setSplitMeterWay(Tools.getDicNameByCodeAndValue(factory, "MRM", user.getSplitMeterWay()));
			}
			// 获取营业所名称
			if (!StringUtils.isEmpty(user.getBusinessAbode())) {
				BusinessAbodeMapper busniessAbodemapper = factory.getMapper(BusinessAbodeMapper.class);
				user.setBusinessAbodeName(
						busniessAbodemapper.findBusinessAbodeById(user.getBusinessAbode()).getAbodeName());
			}
			// 获取营业区域
			if (!StringUtils.isEmpty(user.getBusinessArea())) {
				BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
				user.setBusinessAreaName(
						businessAreaMapper.findBusinessAreaById(user.getBusinessArea(), userInfo.getCurWaterCode()));
			}
			// 获取行政区域
			if (!StringUtils.isEmpty(user.getAdminArea())) {
				DistrictMapper districtMapper = factory.getMapper(DistrictMapper.class);
				user.setAdminAreaName(districtMapper.findDistrictById(user.getAdminArea()).getSname());
			}
			// 获取行业分类名称
			if (!StringUtils.isEmpty(user.getTradeClassify())) {
				TradeClassifyMapper tradeMapper = factory.getMapper(TradeClassifyMapper.class);
				user.setTradeClassifyName(tradeMapper.selectTradeClassifyById(user.getTradeClassify()).getTradeName());
			}
			// 获取优惠策略名称
			if (!StringUtils.isEmpty(user.getPrefStrategyId())) {
				PrefStrategyMapper prefStrategyMapper = factory.getMapper(PrefStrategyMapper.class);
				user.setPrefStrategyName(
						prefStrategyMapper.selectPrefStrategyById(user.getPrefStrategyId()).getStrategyName());
			}
			//获取用水类型
			if(!StringUtils.isBlank(user.getUseWaterType())) {
				WaterTypeMapper waterTypeMapper=factory.getMapper(WaterTypeMapper.class);
				user.setUseWaterTypeName(waterTypeMapper.findWaterTypeById(user.getUseWaterType()).getWaterTypeName());
			}
			info.setData(user);
			return info;
		} catch (Exception e) {
			logger.error("客户管理列表查询失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
